<?php

class Manga_model extends CI_Model {

    function __construct() {
        parent::__construct();
    }

    /**
     * Model to get all manga available
     * @return type 
     */
    function getManga_all() {
//        $d = strtotime('-3 day');
//        $query = $this->db->get_where('manga', array('update <=' => $d));
        $query = $this->db->get('manga');
        $arr = array();
        foreach ($query->result() as $row) {
            $arr[] = $row;
        }
        return $arr;
    }

    function getManga_by_id($id) {
        $manga = $this->db->get_where('manga', array('id' => $id))->row();
        $chapter = $this->mongo_db->order_by(array("chapter" => "asc"))->get('manga_' . $id);
        $manga->chapter = array();
        foreach ($chapter as $row)
            $manga->chapter[] = $row;

        function sortChapter($a, $b) {
            if ($a['chapter'] == $b['chapter']) {
                return 0;
            }
            return ($a['chapter'] < $b['chapter']) ? -1 : 1;
        }

        usort($manga->chapter, 'sortChapter');
        return $manga;
    }

    function getManga_slug_by_id($mangaId) {
        return $this->db->select('slug')->get_where('manga', array('id' => $mangaId))->row()->slug;
    }
    
    function getManga_id_by_slug($slug) {
        return $this->db->select('id')->get_where('manga', array('slug' => $slug))->row()->id;
    }
}

?>
